/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.WhosOnlineModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 *  
 */
public class WhosOnlineComponent extends EditTableComponent
{	
	protected List<WhosOnlineModel> whosOnlineModels;

	/**
	 * Constructor
	 */
	public WhosOnlineComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		whosOnlineModels = new ArrayList<WhosOnlineModel>();
		paginate = true;
		tableType = "visitors";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		whosOnlineModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		WhosOnlineModel model = new WhosOnlineModel(jsonObject);
		whosOnlineModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = whosOnlineModels.size();
		numberOfTableColumns = 6;
		extraRows = 0;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Online");
		grid.setText(0, 1, "Visitor");
		grid.setText(0, 2, "IP Address");
		grid.setText(0, 3, "Entry Time");
		grid.setText(0, 4, "Last Click");
		grid.setText(0, 5, "Last URL");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, whosOnlineModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
	}

	public void displayRow(Grid grid, int row, WhosOnlineModel model) {
		Date date = new Date(model.getLastClickTime().getTime() - model.getEntryTime().getTime());
		//Date date = new Date(0);
		grid.setText(row, 0, CommonUtils.smallTimeFormat.format(date));
		grid.setText(row, 1, model.getFullName());
		grid.setText(row, 2, model.getIpAddress());
		grid.setText(row, 3, CommonUtils.timeFormat.format(model.getEntryTime()));
		grid.setText(row, 4, CommonUtils.timeFormat.format(model.getLastClickTime()));
		grid.setText(row, 5, model.getLastUrl());
	}

	public List<WhosOnlineModel> getWhosOnlineModels() {
		return whosOnlineModels;
	}
	
	public void updateButtonPanel() {
		if (buttonPanel == null)
		{
			buttonPanel = new HorizontalPanel();
			buttonPanel.setStyleName("ZButtons");

			editButton = new Button("refresh", editHandler);
			editButton.addStyleName("ComponentLink");
			buttonPanel.add(editButton);
		}
		editButton.setVisible(true);
	}	
	
	protected ClickHandler editHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "";
			build(true);
		}
	};
}
